package djrain.testPictureFromGallery;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class testPictureFromGallery extends Activity implements OnClickListener
{
	private static final int CAMERA_ACTIVITY1 = 1001;
	private static final int CAMERA_ACTIVITY2 = 1002;
	private static final int CAMERA_ACTIVITY3 = 1003;
	private static final int CAMERA_ACTIVITY4 = 1004;
	private ImageView iv1;
	private ImageView iv2;
	private ImageView iv3;
	private ImageView iv4;
	private ImageView iv5;
	private ImageView iv6;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		findViewById(R.id.Button01).setOnClickListener(this);
		findViewById(R.id.Button02).setOnClickListener(this);
		findViewById(R.id.Button03).setOnClickListener(this);
		findViewById(R.id.Button04).setOnClickListener(this);
		findViewById(R.id.Button05).setOnClickListener(this);
		findViewById(R.id.Button06).setOnClickListener(this);
		iv1 = (ImageView) findViewById(R.id.ImageView01);
		iv2 = (ImageView) findViewById(R.id.ImageView02);
		iv3 = (ImageView) findViewById(R.id.ImageView03);
		iv4 = (ImageView) findViewById(R.id.ImageView04);
		iv5 = (ImageView) findViewById(R.id.ImageView05);
		iv6 = (ImageView) findViewById(R.id.ImageView06);
	}

	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.Button01:
			getPictureFromGallery1();
			break;
		case R.id.Button02:
			getPictureFromGallery2();
			break;
		case R.id.Button03:
			getPictureFromGallery3();
			break;
		case R.id.Button04:
			getPictureFromGallery4();
			break;
		case R.id.Button05:
			// getPictureFromGallery5();
			break;
		case R.id.Button06:
			// getPictureFromGallery6();
			break;
		}
	}

	public void getPictureFromGallery1()
	{
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(intent, CAMERA_ACTIVITY1);
	}

	public void getPictureFromGallery2()
	{
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
		startActivityForResult(intent, CAMERA_ACTIVITY2);
	}

	public void getPictureFromGallery3()
	{
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		// Intent intent = new Intent(Intent.ACTION_PICK);//or
		intent.setType("image/*");
		startActivityForResult(intent, CAMERA_ACTIVITY3);
	}

	public void getPictureFromGallery4()
	{
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		// Intent intent = new Intent(Intent.ACTION_PICK);//or
		intent.setType("image/*");
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1); // 확대축소 비율
		intent.putExtra("aspectY", 1);

		intent.putExtra("outputX", 155);
		intent.putExtra("outputY", 155);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, CAMERA_ACTIVITY4);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode != Activity.RESULT_OK)
			return;

		if (CAMERA_ACTIVITY1 == requestCode)
		{
			Uri uri = data.getData();
			Log.e("djrain", "testPictureFromGallery onActivityResult:uri:" + uri);
			// Bitmap bitmap = (Bitmap) data.getExtras().get("data"); or
			Bitmap bitmap = (Bitmap) data.getParcelableExtra("data");
			Log.e("djrain", "testPictureFromGallery onActivityResult:bitmap:" + bitmap);
			iv1.setImageBitmap(bitmap);
			Toast.makeText(this, "Small image", Toast.LENGTH_SHORT).show();
			MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null, null);
		}

		if (CAMERA_ACTIVITY2 == requestCode)
		{
			Uri uri = data.getData();
			Log.e("djrain", "testPictureFromGallery onActivityResult:uri:" + uri);
			// Bitmap bitmap = (Bitmap) data.getExtras().get("data"); or
			Bitmap bitmap = (Bitmap) data.getParcelableExtra("data");
			Log.e("djrain", "testPictureFromGallery onActivityResult:bitmap:" + bitmap);
			iv2.setImageBitmap(bitmap);
			Bundle b = data.getExtras();
			if (b.containsKey(MediaStore.EXTRA_OUTPUT))
			{
				Toast.makeText(this, "Large image", Toast.LENGTH_SHORT).show();
				MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null, null);
			}
		}

		if (CAMERA_ACTIVITY3 == requestCode)
		{
			Uri uri = data.getData();
			Log.e("djrain", "testPictureFromGallery onActivityResult:uri:" + uri);
			try
			{
				Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
				iv3.setImageBitmap(bitmap);
			}
			catch (FileNotFoundException e)
			{
				e.printStackTrace();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		if (CAMERA_ACTIVITY4 == requestCode)
		{
			Uri uri = data.getData();
			Log.e("djrain", "testPictureFromGallery onActivityResult:uri:" + uri);
			Bitmap bitmap = (Bitmap) data.getParcelableExtra("data");
			Log.e("djrain", "testPictureFromGallery onActivityResult:bitmap:" + bitmap);
			iv4.setImageBitmap(bitmap);
		}
	}
}